package com.danicsoft.daide.command;

import com.danicsoft.daide.token.TokenMap;
import com.danicsoft.daide.token.TokenGroup;


/**
 * Implements the MAP command. From Eric Wald's <a href="http://pydip.brainshell.org/docs/syntax.html">protocol specification</a>, v0.14:
 * <h4><a name="MAP">server_command = MAP</a> ('name')</h4>
 * <p>
 * Once the client has joined the game, the <b>MAP</b> token is sent to specify
 * the map which is to be used, as soon as the <b>HLO</b> or <b>OBS</b> has
 * been received and acknowledged. The name is unique to the map &mdash; so if
 * the client has played a game on a map with this name before, then this will
 * be the same map again. The Standard map is called 'standard'. <b>name</b>
 * <!-- will never be more than 8 characters, and --> can include only letters,
 * numbers and underscore. Map names are not case sensitive.
 * </p>
 * <!--
 * <h4>client_message = MAP</h4>
 * -->
 * <p>
 * The client may request another copy of the <b>MAP</b> message at any time,
 * by sending <b>MAP</b> with no parameters.
 * </p>
 * <p>
 * The client should respond with <b>YES (MAP ('name'))</b> if it is to play on
 * the map, or <b>REJ (MAP ('name'))</b> if the client can not cope with the
 * requested map.
 * </p>
 * <p>
 * If the client does not know the map, then instead of replying with <b>YES
 * (MAP ('name'))</b> or <b>REJ (MAP ('name'))</b>, it can request the map
 * definition with:
 * </p>
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 */
public class MapCommand extends QueryableCommand {

	/**
	 * The name of the map for this game.
	 */
	public String mapName;

	/**
	 * Construct a new <code>MapCommand</code> from the
	 * <code>TokenGroup</code> tokens
	 * 
	 * @param tokens
	 *        A <code>TokenGroup</code> that contains the data for this
	 *        command. It should consist of a single <code>TokenGroup</code>
	 *        whose sole child is a <code>TextToken</code> containing the name
	 *        of the map.
	 * @throws ParseException
	 *         If there was a problem parsing the <code>TokenGroup</code>
	 */
	public MapCommand(TokenGroup tokens) throws ParseException {
		super(tokens);

	}

	/**
	 * Construct a new <code>MapCommand</code> for sending to the a client.
	 * 
	 * @param name
	 *        A String representing the name of the map.
	 */
	public MapCommand(String name) {
		super(false);
		mapName = name;

	}

	@Override
	public String toString() {
		return "Map name: " + mapName;
	}

	@Override
	public void doNonQueryIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
		mapName = tokenIO.doText(tokenIO.subGroup(tokens), mapName);

	}

	@Override
	public byte getType() {
		return TokenMap.Commands.MAP;
	}
}
